package com.zx.crowdfunding.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.zx.crowdfunding.entity.po.MemberConfirmInfoPO;
import com.zx.crowdfunding.entity.po.MemberLaunchInfoPO;
import com.zx.crowdfunding.entity.po.ProjectPO;
import com.zx.crowdfunding.entity.po.ReturnPO;
import com.zx.crowdfunding.entity.vo.DetailProjectVO;
import com.zx.crowdfunding.entity.vo.MemberConfirmInfoVO;
import com.zx.crowdfunding.entity.vo.MemberLaunchInfoVO;
import com.zx.crowdfunding.entity.vo.PortalTypeVO;
import com.zx.crowdfunding.entity.vo.ProjectVO;
import com.zx.crowdfunding.entity.vo.ReturnVO;
import com.zx.crowdfunding.mapper.MemberConfirmInfoPOMapper;
import com.zx.crowdfunding.mapper.MemberLaunchInfoPOMapper;
import com.zx.crowdfunding.mapper.ProjectItemPicPOMapper;
import com.zx.crowdfunding.mapper.ProjectPOMapper;
import com.zx.crowdfunding.mapper.ReturnPOMapper;
import com.zx.crowdfunding.service.api.ProjectService;

/**
 * 项目 - Service接口实现类
 * @author 郑雪
 * @date 2022-01-01
 */
@Service
// 针对查询操作设置事务只读属性
@Transactional(readOnly = true)
public class ProjectServiceImpl implements ProjectService {
	
	/**
	 * 项目Mapper接口
	 */
	@Autowired
	private ProjectPOMapper projectPOMapper;
	
	/**
	 * 项目详情图片Mapper接口
	 */
	@Autowired
	private ProjectItemPicPOMapper projectItemPicPOMapper;
	
	/**
	 * 项目发起人信息Mapper接口
	 */
	@Autowired
	private MemberLaunchInfoPOMapper memberLaunchInfoPOMapper;
	
	/**
	 * 项目发起人确认信息Mapper接口
	 */
	@Autowired
	private MemberConfirmInfoPOMapper memberConfirmInfoPOMapper;
	
	/**
	 * 项目回报信息Mapper接口
	 */
	@Autowired
	private ReturnPOMapper returnPOMapper;

	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
	public void saveProject(ProjectVO projectVO, Integer memberId) {
		
		// 一、保存ProjectPO对象
		// 1.创建空的ProjectPO对象
		ProjectPO projectPO = new ProjectPO();
		
		// 2.把ProjectVO对象中的属性复制到ProjectPO对象中
		BeanUtils.copyProperties(projectVO, projectPO);
		projectPO.setStatus(0);
		projectPO.setSupportMoney(0L);
		projectPO.setSupporter(0);
		projectPO.setCompletion(0);
		projectPO.setMemberId(memberId);
		projectPO.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		projectPO.setFollower(0);
		
		// 3.保存ProjectPO对象
		projectPOMapper.insertSelective(projectPO);
		
		// 4.从ProjectPO对象中获取自增的主键
		Integer projectId = projectPO.getId();
		
		// 二、保存项目、分类的关联关系信息
		// 1.从ProjectVO对象中获取项目分类id集合
		List<Integer> typeIdList = projectVO.getTypeIdList();
		
		// 2.保存
		projectPOMapper.insertProjectTypeRelationship(typeIdList, projectId);
		
		
		// 三、保存项目、标签的关联关系信息
		// 1.从ProjectVO对象中获取项目标签id集合
		List<Integer> tagIdList = projectVO.getTagIdList();
		
		// 2.保存
		projectPOMapper.insertProjectTagRelationship(tagIdList, projectId);
		
		
		// 四、保存项目中详情图片路径的信息
		// 1.从ProjectVO对象中获取项目详情图片路径的集合
		List<String> detailPicturePathList = projectVO.getDetailPicturePathList();
		
		// 2.保存
		projectItemPicPOMapper.insertPathList(detailPicturePathList, projectId);
		
		// 五、保存项目发起人信息
		// 1.从ProjectVO对象中获取项目发起人信息对象
		MemberLaunchInfoVO memberLaunchInfoVO = projectVO.getMemberLaunchInfoVO();
		
		// 2.创建空的MemberLaunchInfoPO对象
		MemberLaunchInfoPO memberLaunchInfoPO = new MemberLaunchInfoPO();
		
		// 3.把MemberLaunchInfoVO对象中的属性复制到MemberLaunchInfoPO对象中
		BeanUtils.copyProperties(memberLaunchInfoVO, memberLaunchInfoPO);
		
		// 4.给MemberLaunchInfoPO对象的会员id属性赋值
		memberLaunchInfoPO.setMemberId(memberId);
		
		// 5.保存MemberLaunchInfoPO对象
		memberLaunchInfoPOMapper.insertSelective(memberLaunchInfoPO);
		
		// 六、保存项目回报信息
		// 1.从ProjectVO对象中获取项目回报信息集合
		List<ReturnVO> returnVOList = projectVO.getReturnVOList();
		
		// 2.创建空的ReturnPO对象集合
		List<ReturnPO> returnPOList = new ArrayList<>();
		
		// 3.遍历
		for (ReturnVO returnVO : returnVOList) {
			ReturnPO returnPO = new ReturnPO();
			BeanUtils.copyProperties(returnVO, returnPO);
			returnPOList.add(returnPO);
		}
		
		// 4.保存
		returnPOMapper.insertReturnPOList(returnPOList, projectId);
		
		// 七、保存项目发起人确认信息
		// 1.从ProjectVO对象中获取发起人确认信息对象
		MemberConfirmInfoVO memberConfirmInfoVO = projectVO.getMemberConfirmInfoVO();
		
		// 2.创建空的MemberConfirmInfoPO对象
		MemberConfirmInfoPO memberConfirmInfoPO = new MemberConfirmInfoPO();
		
		// 3.把MemberConfirmInfoVO对象中的属性复制到MemberConfirmInfoPO对象中
		BeanUtils.copyProperties(memberConfirmInfoVO, memberConfirmInfoPO);
		
		// 4.给MemberConfirmInfoPO对象中的会员id属性赋值
		memberConfirmInfoPO.setMemberId(memberId);
		
		// 5.保存MemberConfirmInfoPO对象
		memberConfirmInfoPOMapper.insertSelective(memberConfirmInfoPO);
		
		
	}

	@Override
	public List<PortalTypeVO> getPortalTypeVOList() {
		return projectPOMapper.selectPortalTypeVOList();
	}

	@Override
	public DetailProjectVO getDetailProjectVO(Integer projectId) {
		
		// 1.查询得到DetailProjectVO对象
		DetailProjectVO detailProjectVO = projectPOMapper.selectDetailProjectVO(projectId);
		
		// 2.根据status确定statusText
		Integer status = detailProjectVO.getStatus();
		
		switch (status) {
		case 0:
			detailProjectVO.setStatusText("即将开始");
			break;
		case 1:
			detailProjectVO.setStatusText("众筹中");
			break;
		case 2:
			detailProjectVO.setStatusText("众筹成功");
			break;
		case 3:
			detailProjectVO.setStatusText("众筹失败");
			break;
		default:
			break;
		}
		
		try {
			
			// 3.根据deployDate计算lastDay
			String deployDateString = detailProjectVO.getDeployDate();
			Date deployDate = new SimpleDateFormat("yyyy-MM-dd").parse(deployDateString);
			Integer day = detailProjectVO.getDay();
			
			// 计算结束日期时间戳
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(deployDate);
			calendar.add(Calendar.DAY_OF_YEAR, day);
			Date endDate = calendar.getTime();
			long endTime = endDate.getTime();
			
			// 获取当前日期时间戳
			Long currentTime = System.currentTimeMillis();
			
			// 计算剩余天数
			Integer lastDay = (int) ((endTime - currentTime) / (1000 * 60 * 60 * 24));
			detailProjectVO.setLastDay(lastDay);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return detailProjectVO;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
	public void editProjectProgress(Integer projectId) {
		projectPOMapper.updateProjectProgress(projectId);
	}
	

}
